package com.futu.openapi.sample.mine.utils;

import com.futu.openapi.sample.mine.model.Mail;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 * @author: xiaomi
 * @date: 2022/1/9
 * @description:
 */
public class MailUtil {


    /**
     * 发送邮件
     * @param mail
     * @return
     * @throws EmailException
     */
    public static boolean send(Mail mail) throws EmailException {
        // 发送email
        HtmlEmail email = new HtmlEmail();
        ///SMTP发送服务器的名字
        email.setHostName(mail.getHost());
        ///字符编码集的设置
        email.setCharset(Mail.ENCODING);
        ///email.setSmtpPort(mail.getPort());
        ///设置 SSL SMTP 端口
        email.setSslSmtpPort(mail.getPort().toString());
        ///如果需要认证信息的话，设置认证：用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
        email.setAuthentication(mail.getUsername(), mail.getPassword());
        ///发送人的邮箱
        email.setFrom(mail.getSender(), mail.getName());
        ///要发送的邮件主题
        email.setSubject(mail.getSubject());
        ///启用 SSL
        email.setSSLOnConnect(true);
        if (mail.getMessageTxt() != null) {
            email.setMsg(mail.getMessageTxt());
        }
        if (mail.getMessageHtml() != null) {
            email.setHtmlMsg(mail.getMessageHtml());
        }
        for (String str : mail.getReceivers()) {
            email.addTo(str);
        }

        email.send();
        return true;
    }
}
